package com.itheima.servlet;


import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@WebServlet(value="/demo1",loadOnStartup = 1)
public class ServletDemo1 implements Servlet {
    private ServletConfig config;
    /**
     *  初始化servlet对象
     * 默认serlvet第一次被调用时,调用吧
     *  *loadOnStartup: >=0 时,服务器启动时就会被创建
     * 只会被调用一次
     * @param servletConfig
     * @throws ServletException
     */
    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        this.config = config;
    }

    /**
     *
     * @return
     */
    @Override
    public ServletConfig getServletConfig() {
        return config;
    }

    /**
     * 每一次调用serlvet都会调用这个方法
     * @param servletRequest
     * @param servletResponse
     * @throws ServletException
     * @throws IOException
     */
    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        //不同请求的参数位置不同,因此要根据不同的请求方法,进行分别处理
        HttpServletRequest request = (HttpServletRequest)servletRequest;
        //获取请求方法
        String method = request.getMethod();
        //判断
        if ("GET".equals(method)){
            doGet(servletRequest,servletResponse);
        }else if("POST".equals(method)){
            doPost(servletRequest,servletResponse);
        }
    }

    protected void doPost(ServletRequest servletRequest, ServletResponse servletResponse) {
        System.out.println("do post");
    }

    protected void doGet(ServletRequest servletRequest, ServletResponse servletResponse) {
        System.out.println("do get");
    }

    /**
     * 描述项目的信息,如版本信息和作者等等
     * @return
     */
    @Override
    public String getServletInfo() {
        return null;
    }

    /**
     * 销毁方法
     *  1.内存释放或者服务器关闭时,自动调用
     *  只会调用一次
     */
    @Override
    public void destroy() {

    }
}
